package com.zdsoft.finance.product.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.zdsoft.finance.product.entity.ProductMaterialList;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 产品资料清单自定义仓库实现
 * @author longwei
 * @date 2016/11/22
 * @version 1.0
 */
public class ProductMaterialListRepositoryImpl{
	//查询头部
	private StringBuffer hqlBuffer = new StringBuffer("from ProductMaterialList where 1=1 ");
	
	@PersistenceContext
	private EntityManager em;
	
	/**
	 * 通过资料清单对象查询列表
	 * @param productMaterialList 资料清单对象
	 * @param pageable 分页信息
	 * @return 分页结果
	 */
	@SuppressWarnings("unchecked")
	public Page<ProductMaterialList> queryByCons(ProductMaterialList productMaterialList, Pageable pageable){
		//条件组装
		StringBuffer whereHqlBuffer = new StringBuffer(" and isDeleted=:isDeleted");
		if(ObjectHelper.isNotEmpty(productMaterialList.getName())){
			whereHqlBuffer.append(" and name like :name ");
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getTypeCd())){
			whereHqlBuffer.append(" and typeCd = :typeCd");
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getClientTypeCd())){
			whereHqlBuffer.append(" and clientTypeCd = :clientTypeCd");
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getProduct().getId())){
			whereHqlBuffer.append(" and product.id = :productId");
		}
		whereHqlBuffer.append(" order by createTime desc");
		
		//参数组装
		Query query = em.createQuery(this.hqlBuffer.toString() + whereHqlBuffer.toString());
		Query countQ = em.createQuery("select count(*) " + this.hqlBuffer.toString() + whereHqlBuffer.toString());
		query.setParameter("isDeleted",!BaseEntity.DELETED);
		countQ.setParameter("isDeleted", !BaseEntity.DELETED);
		
		if(ObjectHelper.isNotEmpty(productMaterialList.getName())){
			query.setParameter("name","%"+productMaterialList.getName()+"%");
			countQ.setParameter("name","%"+ productMaterialList.getName()+"%");
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getTypeCd())){
			query.setParameter("typeCd",productMaterialList.getTypeCd());
			countQ.setParameter("typeCd", productMaterialList.getTypeCd());
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getClientTypeCd())){
			query.setParameter("clientTypeCd",productMaterialList.getClientTypeCd());
			countQ.setParameter("clientTypeCd", productMaterialList.getClientTypeCd());
		}
		if(ObjectHelper.isNotEmpty(productMaterialList.getProduct()) && ObjectHelper.isNotEmpty(productMaterialList.getProduct().getId())){
			query.setParameter("productId",productMaterialList.getProduct().getId());
			countQ.setParameter("productId",productMaterialList.getProduct().getId());
		}
		
		//查询
		query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
		query.setMaxResults(pageable.getPageSize());
		Page<ProductMaterialList> pager = new PageImpl<>(query.getResultList(),pageable,(Long)countQ.getSingleResult());
		return pager;
	}
}
